<!DOCTYPE html>
<html class="no-js" lang="">

<head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <title>Ai - Chat</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
        #chatgpt-response {
            font-family: "宋体";
            font-size: 20px;
            color: #0000FF;
            font-weight: bold;
        }
    </style>

    <script>
        async function callSay() {
            var responseText1 = document.getElementById("chatgpt-response");
            responseText1.innerHTML = ""

            function printMessage(message) {
                var responseText = document.getElementById("chatgpt-response");
                var index = 0;

                // 创建一个定时器，每隔一段时间打印一个字符
                var interval = setInterval(function () {
                        responseText.innerHTML += message[index];
                        index++;

                        // 当打印完成时，清除定时器
                        if (index >= message.length) {
                            clearInterval(interval);
                        }
                    },
                    150); // 每隔50毫秒打印一个字符
            }

            var xhr = new XMLHttpRequest();
            var url = "v1/chat/completions";
            xhr.open("POST", url, true);
            xhr.setRequestHeader("Content-Type", "application/json");
            xhr.setRequestHeader("Authorization", "Bearer API-KEY");
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    var json = JSON.parse(xhr.responseText);
                    var response = json.choices[0].text;

                    // 将CHATGPT的返回值输出到文本框
                    var responseText = document.getElementById("chatgpt-response");
                    var index = 0;

                    // 创建一个定时器，每隔一段时间打印一个字符
                    var interval = setInterval(function () {
                            responseText.innerHTML += response[index];
                            index++;

                            // 当打印完成时，清除定时器
                            if (index >= response.length) {
                                clearInterval(interval);
                            }
                        },
                        50); // 每隔50毫秒打印一个字符
                }
            };

            var data = JSON.stringify({
                "text": document.getElementById("chat-gpt-input").value
            });
            console.log(data);
            await printMessage('正在思考，请等待......');
            await xhr.send(data);
        }
    </script>
</head>

<body>
<div id="content" style="width: 800px; margin: 0 auto; padding: 0;">
    <div class="filter-menu text-center mb-40">
        <h4>与Ai对话，请描述您的需求-支持中文、英语、日本语等</h4>
    </div>

    <textarea class="form-control" id="chat-gpt-input" placeholder="输入描述" rows="3" resize="none"
              style="width: 80%; min-width: 300px; margin: 0 auto; background-color: #f4f4f4; color: #333; border: 1px solid #ccc; border-radius: 5px;"></textarea>
    <button onclick="callSay()" autocomplete="off" class="btn btn-large" href="#"
            style="background-color: #0000FF; color: #ffffff; border-radius: 5px">
        <span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>回答
    </button>
    <textarea class="form-control" id="chatgpt-response"
              placeholder="请耐心等待回答 Ai生成它很快，但是由于网络问题我们需要等待，通常内容越长等待越久 如果长时间没反应请刷新页面重试" rows="26" resize="none"
              style="width: 80%;min-width: 300px;height: auto; margin: 0 auto; background-color: #f4f4f4; color: #333; border: 1px solid #ccc; border-radius: 5px; overflow: scroll;"
              readonly="true"></textarea>
</div>
</body>
</html>